<?php
namespace Admin\Service;
//用户 service
use Admin\AppConst\WebConst;
use Admin\Model\AdminModel;
use Think\Page;

class AdminService extends BaseService
{

    /**
     * @var AdminService
     */
    private static $instance;

    /**
     * Get the instance of AdminService.
     * @return AdminService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new AdminService();
        }
        return self::$instance;
    }


    /**
     * @function    createAccount  手机注册
     * @param       $email      string  邮箱
     * @param       $password   string  密码
     * @param       $name       string  姓名
     * @param       $roleId     int     权限
     * @author      zhangle
     * @date        2018-07-30
     * @return      array
     */
    public function createAccount($email, $password, $name, $roleId){
        $result = array(
            'status'    => WebConst::DEFAULT_TYPE_TRUE,
        );

        //检测用户是否存在
        $accountInfo   = $this->getAccountByEmail($email, WebConst::DEFAULT_TYPE_FALSE);
        if(!empty($accountInfo['id'])){
            return array(
                'status'    => WebConst::DEFAULT_TYPE_FALSE,
                'message'   => $email. '已经被注册过了, 请换一个邮箱创建',
            );
        }

        //开始注册
        //salt
        $salt       = getRandString(WebConst::USER_SALT_LEN, WebConst::DEFAULT_TYPE_TRUE);
        $password   = md5($password.$salt);
        //开始注册
        $userId     = AdminModel::get()->createAccount($email, $password, $salt, $name, $roleId);


        if($userId < WebConst::DEFAULT_TYPE_STATUS_TRUE){
            return array(
                'status'    => WebConst::DEFAULT_TYPE_FALSE,
                'message'   => $email. '创建失败',
            );
        }

        return $result;
    }

    /**
     * @function    editAccount  更新密码
     * @param       $id         int     id
     * @param       $name       string  姓名
     * @param       $roleId     int     权限
     * @param       $isUpdatePassword   bool    是否修改密码
     * @param       $password   string  密码
     * @author      zhangle
     * @date        2018-07-30
     * @return      bool
     */
    public function editAccount($id, $name, $roleId, $isUpdatePassword, $password){
        //开始注册
        if($isUpdatePassword == true){
            //salt
            $salt       = getRandString(WebConst::USER_SALT_LEN, WebConst::DEFAULT_TYPE_TRUE);
            $password   = md5($password.$salt);
            //开始注册
            return AdminModel::get()->editAccount($id, $name, $roleId, $isUpdatePassword, $password, $salt);
        }else{
            //开始注册
            return AdminModel::get()->editAccount($id, $name, $roleId, $isUpdatePassword);
        }

    }

    /**
     * @function    mobileLogin 手机登录
     * @param       $email      string  邮箱
     * @param       $password   string  密码
     * @author      zhangle
     * @date        2018-07-30
     * @return      array
     */
    public function login($email, $password){
        $result = array(
            'status'    => WebConst::DEFAULT_TYPE_TRUE,
        );

        //检测用户是否存在
        $accountInfo   = $this->getAccountByEmail($email);
        if(empty($accountInfo['id'])){
            return array(
                'status'    => WebConst::DEFAULT_TYPE_FALSE,
                'message'   => $email. '邮箱不存在',
            );
        }
        //验证密码
        if($accountInfo['password'] !== md5($password.$accountInfo['salt'])){
            return array(
                'status'    => WebConst::DEFAULT_TYPE_FALSE,
                'message'   => $email. '密码错误',
            );
        }

        //查询权限
        //查询权限分组
        $role                       = RoleService::get()->getRoleById($accountInfo['role_id']);
        $accountInfo['menuList']    = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        if(!empty($role)){
            $accountInfo['menuList']   = explode(',', $role['menu_ids']);
        }

        //注册成功, 组织需要返回的数据
        session(WebConst::USER_SESSION_KEY, $accountInfo);
        return $result;
    }




    /**
     * @function    getAccountByUserId  返回用户信息
     * @param       $userId     int     用户id
     * @author      zhangle
     * @date        2018-07-30
     * @return      array
     */
    private function getAccountByUserId($userId){
        return AdminModel::get()->getAccountByUserId($userId);
    }


    /**
     * @function    getAccountByMobile  通过手机号码查询用户信息
     * @param       $email      string  手机号
     * @param       $checkIsDel bool    是否过滤冻结用户
     * @author      zhangle
     * @date        2018-07-30
     * @return      array
     */
    public function getAccountByEmail($email, $checkIsDel = WebConst::DEFAULT_TYPE_TRUE){
        return AdminModel::get()->getAccountByEmail($email, $checkIsDel);
    }

    /**
     * @function    deleteAccount  删除
     * @param       $id         int  id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function deleteAccount($id){
        return AdminModel::get()->deleteAccount($id);
    }


    /**
     * @function    getList     获取所有用户
     * @author      zhangle
     * @date        2018-08-02
     * @return      array
     */
    public function getList(){
        return AdminModel::get()->getList();
    }

    /**
     * @function    getList     获取所有用户
     * @author      zhangle
     * @date        2018-08-02
     * @return      array
     */
    public function getAppAccountList(){
        return AdminModel::get()->getList();
    }
}